package com.outsourcing.bottle.util;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.outsourcing.bottle.R;
import com.outsourcing.bottle.ui.HomeActivity;
/**
 * 
 * @author 06peng
 *
 */
public class BottleService extends Service {
	
	private ServiceBinder serviceBinder = new ServiceBinder();

	@Override
	public IBinder onBind(Intent intent) {
		return serviceBinder;
	}

	@Override
	public void onStart(Intent intent, int startId) {
		showNotification(getString(R.string.app_name));
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		super.onStartCommand(intent, flags, startId);
		return START_STICKY;
	}
	
	@SuppressWarnings("deprecation")
	public void showNotification(String mes) {
		CharSequence from = mes;
		CharSequence message = "";
		Intent intent;
		if (AppContext.getContext() == null) {
			intent = new Intent(this, HomeActivity.class);
		} 
		else {
			intent = new Intent(this, AppContext.getContext().getClass());
		}
		intent.setAction("android.intent.action.MAIN");
		intent.addCategory("android.intent.category.LAUNCHER");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);
		Notification notif = new Notification(R.drawable.logo, mes, System.currentTimeMillis());
		notif.flags = Notification.FLAG_ONGOING_EVENT;
		notif.setLatestEventInfo(this, from, message, contentIntent);
		this.startForeground(R.drawable.logo, notif);
	}
	
	public class ServiceBinder extends Binder {
		public BottleService getService() {
			return BottleService.this;
		}
	}
}
